<?php

namespace App\Admin\Controllers;

use App\Admin\Controllers\Helpers\LinkHelper;
use App\Constant\OrderEnum;
use App\Models\Order;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Show;

class OrderController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Order(), function (Grid $grid) {
            $grid->model()->with('user')
                ->whereNotIn('status', [
                    OrderEnum::STATUS_PENDING,
                    OrderEnum::STATUS_TIMEOUT,
                ])
                ->orderBy('id', 'desc');
            $grid->quickSearch('id', 'user_id','user.uuid','user.nickname')->placeholder('输入 id\uuid\用户昵称\uid')->auto(false);;

            $grid->column('id')->sortable();
            $grid->column('user_id')->display(function () {
                return LinkHelper::displayUser($this->user);
            });
            $grid->column('amount')->sortable();
            $grid->column('status')
                ->using(OrderEnum::STATUS_EXPLAINS)
                ->label(OrderEnum::STATUS_COLORS)
                ->sortable();
            $grid->column('order_no')->sortable();
            $grid->column('out_trade_no')->sortable();
            $grid->column('trader_message');
            $grid->column('paid_at')->sortable();
            $grid->column('remark');

            $grid->disableCreateButton();
            $grid->disableActions();
            $grid->disableBatchActions();
            $grid->disableBatchDelete();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Order(), function (Show $show) {
            $show->field('id');
            $show->field('user_id');
            $show->field('type');
            $show->field('amount');
            $show->field('order_no');
            $show->field('out_trade_no');
            $show->field('trader_message');
            $show->field('paid_at');
            $show->field('status');
            $show->field('remark');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Order(), function (Form $form) {
            $form->display('id');
            $form->text('user_id');
            $form->text('type');
            $form->text('amount');
            $form->text('order_no');
            $form->text('out_trade_no');
            $form->text('trader_message');
            $form->text('paid_at');
            $form->text('status');
            $form->text('remark');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
